package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.CsCar;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.CsCarService;
import co.yixiang.modules.system.dto.CsCarDto;
import co.yixiang.modules.system.dto.queryCriteria.CsCarQueryCriteria;
import co.yixiang.modules.system.mapper.CsCarMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author zf
* @date 2021-09-09
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "csCar")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CsCarServiceImpl extends BaseServiceImpl<CsCarMapper, CsCar> implements CsCarService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(CsCarQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<CsCar> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(CsCarDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<CsCar> queryAll(CsCarQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(CsCar.class, criteria));
    }


    @Override
    public void download(List<CsCarDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CsCarDto csCar : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" openId",  csCar.getOpenId());
            map.put("手机号", csCar.getPhone());
            map.put("车总价", csCar.getMoney());
            map.put("企业名称", csCar.getCompany());
            map.put("购买时间", csCar.getPayTime());
            map.put("创建时间", csCar.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
